package com.springboot.cloud.demos.feign.rest;

import com.alibaba.fastjson.JSONObject;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.demos.entity.form.MomentSupportForm;
import com.springboot.cloud.demos.entity.po.MomentSupport;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.feign.config.BusConfig;
import com.springboot.cloud.demos.feign.events.BusSender;
import com.springboot.cloud.demos.feign.service.MomentSupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
public class MomentSupportController extends BaseController {

    @Autowired
    private MomentSupportService momentSupportService;
    @Autowired
    private BusSender busSender;

    @RequestMapping(value = "/momentSupport", method = RequestMethod.POST)
    public Result save(HttpServletRequest request, @RequestBody MomentSupportForm momentSupportForm) {
        User user = getUser(request);
        momentSupportForm.setUserId(user.getId());
        momentSupportForm.setType(0);
        Result result = momentSupportService.save(momentSupportForm);

        if (result.isSuccess()) {
            //新建推送和消息
            String message = JSONObject.toJSONString(momentSupportForm);
            busSender.send(BusConfig.EXCHANGE_NAME, "support.message", message);
        }
        return result;
    }

    @RequestMapping(value = "/momentSupport/signUp", method = RequestMethod.POST)
    public Result signUp(HttpServletRequest request, @RequestBody MomentSupportForm momentSupportForm) {
        User user = getUser(request);
        momentSupportForm.setUserId(user.getId());
        momentSupportForm.setType(1);
        Result result = momentSupportService.save(momentSupportForm);

        if (result.isSuccess()) {
            //新建推送和消息
            String message = JSONObject.toJSONString(momentSupportForm);
            busSender.send(BusConfig.EXCHANGE_NAME, "signUp.message", message);
        }
        return result;
    }

    @RequestMapping(value = "/momentSupport/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") String id) {
        return momentSupportService.delete(id);
    }

    @RequestMapping(value = "/momentSupport/cancelMomentSupport", method = RequestMethod.DELETE)
    public Result cancelMomentSupport(HttpServletRequest request, String momentId, Integer type) {
        User user = getUser(request);
        Result<MomentSupport> result = momentSupportService.queryByUser(user.getId(), type, momentId);
        if (result.isSuccess()) {
            MomentSupport momentSupport = result.getData();
            return momentSupportService.delete(momentSupport.getId());
        }
        return Result.fail();
    }

    @RequestMapping(value = "/momentSupport/querySupport", method = RequestMethod.GET)
    public Result querySupport(HttpServletRequest request, String momentId, Integer type) {
        User user = getUser(request);
        return momentSupportService.queryByUser(user.getId(), type, momentId);
    }

    @RequestMapping(value = "/momentSupport/querySignUpList", method = RequestMethod.GET)
    public Result querySignUpList(HttpServletRequest request, String momentId) {
        return momentSupportService.querySignUpList(momentId);
    }
}
